﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKArchBaseCode.OutWallSearch;
using YJKRevitView;

namespace YJKArchUtils.OutWallSearch
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdOutWallSearchTest1 : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.m_Revit = commandData;
			this.m_Doc = commandData.Application.ActiveUIDocument.Document;
			List<WallData> outWallList = new List<WallData>();
			if (!OutWallFilter.Search(this.m_Doc, this.m_Doc.ActiveView.Id, ref outWallList, true, true))
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			this.IsOutWallBySegment(outWallList);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void IsOutWallBySegment(List<WallData> outWallList)
		{
			Wall wall = null;
			if (!this.PickWall(ref wall))
			{
				return;
			}
			Curve wallCenterLineSegment = null;
			if (!this.PickCenterLine(ref wallCenterLineSegment))
			{
				return;
			}
			int num = outWallList.FindIndex((WallData value) => value.Id.IntegerValue == wall.Id.IntegerValue);
			if (num == -1)
			{
				TaskDialog.Show("ID" + wall.Id.ToString(), "内墙");
				return;
			}
			if (outWallList[num].IsOutWallWithCenterLineSegment(wallCenterLineSegment))
			{
				TaskDialog.Show("ID" + wall.Id.ToString(), "外墙");
				return;
			}
			TaskDialog.Show("ID" + wall.Id.ToString(), "内墙");
		}

		private bool PickWall(ref Wall wall)
		{
			Selection selection = this.m_Revit.Application.ActiveUIDocument.Selection;
			try
			{
				WallFilter wallFilter = new WallFilter();
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, wallFilter, "选择墙");
				Element element = this.m_Doc.GetElement(reference);
				wall = (element as Wall);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private bool PickCenterLine(ref Curve centerLineCurve)
		{
			Selection selection = this.m_Revit.Application.ActiveUIDocument.Selection;
			try
			{
				CenterLineFilter centerLineFilter = new CenterLineFilter(false);
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, centerLineFilter, "选择线");
				DetailCurve detailCurve = this.m_Doc.GetElement(reference) as DetailCurve;
				centerLineCurve = detailCurve.GeometryCurve;
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private ExternalCommandData m_Revit;

		private Document m_Doc;
	}
}
